package cqrtplm.controller;

import com.alibaba.fastjson.JSONObject;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.response.ResponseResult;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.query.RtSegmNumQueryDTO;
import cqrtplm.entity.RtMbomCodeDO;
import cqrtplm.entity.RtSegmNumDO;
import cqrtplm.entity.RtTestRequirementsDO;
import cqrtplm.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;

/*
 *
 *
 * */
@RestController
@Api(tags = "xxx服务")
@RequestMapping(RouteConstant.BOM)
public class BomAssociatedDocController {

    @Resource
    private AssociatedDocService associatedDocService;
    @Resource
    private RtSegmNumService rtSegmNumService;
    @Resource
    private RtTestRequirementsService rtTestRequirementsService;
    @Resource
    private RtCertCombCertService rtCertCombcertService;
    @Resource
    private RTChangeNoticeService rtChangeNoticeService;

    @PostMapping("/docList")
    @ApiOperation(value = "docList", notes = "查询BOM结构中的所有参考文档和描述文档", produces = "application/json; charset=utf-8")
    public ResponseResult docList(@RequestBody List<IdentifierEntity> identifierEntities) {
        return ResponseResult.success(this.associatedDocService.getPartLinkDocAndCadDoc(identifierEntities));
    }
    /**
     * 图文档打包下载
     * @param identifierEntities
     * @return
     */
    @PostMapping("/download-doc-files")
    @ApiOperation(value = "图文档打包下载")
    public ResponseResult<byte[]> downloadStatusFiles(@RequestBody List<IdentifierEntity> identifierEntities) {
        return this.associatedDocService.downloadFilesAsZip(identifierEntities);
    }
    @PostMapping("/exportDocList")
    @ApiOperation(value = "exportDocList", notes = "导出关联图文档清单", produces = "application/json; charset=utf-8")
    public void exportDocList(@RequestBody Object object, HttpServletResponse response) throws IOException {
        this.associatedDocService.exportDocList(object, response);
    }

    @PostMapping("/queryDocListByKeyWord")
    @ApiOperation(value = "queryDocListByKeyWord", notes = "查询图文档清单", produces = "application/json; charset=utf-8")
    public ResponseResult queryDocListByKeyWord(@RequestBody LinkedHashMap map) {
        return ResponseResult.success(this.associatedDocService.queryDocListByKeyWord(map));
    }

    @PostMapping("/docCompleteList")
    @ApiOperation(value = "docCompleteList", notes = "查询BOM结构中图文档的齐套性", produces = "application/json; charset=utf-8")
    public ResponseResult docCompleteList(@RequestBody List<IdentifierEntity> identifierEntities) {
        return ResponseResult.success(this.associatedDocService.getPartLinkCompleteList(identifierEntities));
    }

    @GetMapping("/getRtSegmNum")
    @ApiOperation(value = "getRtSegmNum", notes = "获取段号和装配位置", produces = "application/json; charset=utf-8")
    public ResponseResult getRtSegmNum(String segmNum) {
        List<RtSegmNumDO> list = rtSegmNumService.listAll(new RtSegmNumQueryDTO());
        JSONObject jsonObject = new JSONObject();
        for (RtSegmNumDO rtSegmNumDO : list) {
            String id = rtSegmNumDO.getId();
            if (StringUtils.isNotBlank(segmNum)) {
                if (id.contains(segmNum)) {
                    jsonObject.put(id, rtSegmNumDO.getPosition());
                }
            } else {
                jsonObject.put(id, rtSegmNumDO.getPosition());
            }
        }
        return ResponseResult.success(jsonObject);
    }

    @GetMapping("/getTestRequirements")
    @ApiOperation(value = "getTestRequirements", notes = "获取测试要求信息", produces = "application/json; charset=utf-8")
    public ResponseResult queryAll() {
        List<RtTestRequirementsDO> testRequirementsDOList = rtTestRequirementsService.queryAll();
        JSONObject jsonObject = new JSONObject();
        if (testRequirementsDOList.size() > 0) {
            for (RtTestRequirementsDO rtTestRequirementsDO : testRequirementsDOList) {
                String id = rtTestRequirementsDO.getId();
                String rttestRequirements = rtTestRequirementsDO.getTestRequirements();
                if (StringUtils.isNotBlank(id) && StringUtils.isNotBlank(rttestRequirements)) {
                    jsonObject.put(id, rttestRequirements);
                }
            }
        }
        return ResponseResult.success(jsonObject);
    }

    @GetMapping("/getRTAccreditationType")
    @ApiOperation(value = "getRTAccreditationType", notes = "获取所有认证类型", produces = "application/json; charset=utf-8")
    public ResponseResult getRTAccreditationType() {
        return ResponseResult.success(this.rtCertCombcertService.queryAll());
    }

    @GetMapping("/getMBOMStatus")
    @ApiOperation(value = "getMBOMStatus", notes = "查询成品码的状态", produces = "application/json; charset=utf-8")
    public ResponseResult getMBOMStatus(@RequestParam String mbomcode) {
        Object object = this.rtChangeNoticeService.getMBOMStatus(mbomcode);
        if (object != null && object instanceof RtMbomCodeDO) {
            return ResponseResult.success(object);
        }else{
            return ResponseResult.fail("400",object.toString());
        }
    }

}
